<?php

namespace Desktopd\MUA;

// NewMessage (Raw MIME)
class NewMessage extends Message implements \Countable {
    protected $from;
    protected $replyTo = false;
    
    protected $subject = '';
    
    protected $toList = array();
    protected $ccList = array();
    protected $bccList = array();
    
    public function __construct (EmailAddress $from = null, EmailAddress $replyTo = null) {
        $this->setFrom($from);
        
        if ($replyTo instanceof EmailAddress) {
            $this->setReplyTo($replyTo);
        } else {
            $this->setReplyTo($from);
        }
        
        
    }
    
    public function setFrom (EmailAddress $from = null) {
        if ($from instanceof EmailAddress) {
            return $this->from = $from;
        }
        
        return false;
    }
    
    public function setReplyTo (EmailAddress $replyTo = null) {
        if ($replyTo instanceof EmailAddress) {
            return $this->replyTo = $replyTo;
        }
        
        return false;
    }
    
    public function addTo (EmailAddress $to) {
        $this->toList[$to->getAddress()] = $to;
    }
    
    public function addCc (EmailAddress $cc) {
        $this->ccList[$cc->getAddress()] = $cc;
    }
    
    public function addBcc (EmailAddress $bcc) {
        $this->bccList[$bcc->getAddress()] = $bcc;
    }
    
    // add a recipient without modifying the header -- the same thing as Bcc
    public function addRecipient (EmailAddress $recipient) {
        $this->addBcc($recipient);
    }
    
    public function getTo () {
        return $this->toList;
    }
    
    public function getCc () {
        return $this->ccList;
    }
    
    public function getBcc () {
        return $this->bccList;
    }
    
    public function getRecipients () {
        $recipients = array();
        foreach ($this->toList as $to) {
            if (!in_array($to, $recipients, true)) {
                $recipients[] = $to;
            }
        }
        
        foreach ($this->ccList as $cc) {
            if (!in_array($cc, $recipients, true)) {
                $recipients[] = $cc;
            }
        }
        
        foreach ($this->bccList as $bcc) {
            if (!in_array($bcc, $recipients, true)) {
                $recipients[] = $bcc;
            }
        }
        
        return $recipients;
    }
    
    // count recipients (To + Cc + Bcc)
    public function count () {
        return count($this->toList)
            + count($this->ccList)
            + count($this->bccList);
    }
    
    // inline
    public function setText($text = '', $type = 'text/plain; charset="utf-8"', $name = null) {
        $body = Base64::encode($text, Base64::MIME);
        $this->importRaw(<<<EOD
Content-Type: $type
Content-Transfer-Encoding: base64

$body
EOD
        );
    }
    
    // attachment
    public function setAttachment($data, $type = 'text/html; charset="utf-8"', $name = 'attachment') {
        $body = Base64::encode($data, Base64::MIME);
        $type = str_replace(array("\r", "\n"), '', trim($type));
        $ascii = str_replace(array("\r", "\n", '"'), '?', mb_convert_encoding($name, "us-ascii", "UTF-8"));
        $name = rawurlencode($name);
        
        $this->importRaw(<<<EOD
Content-Type: $type;
 name="$ascii"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
 filename="$ascii"
 filename*=UTF-8''$name

$body
EOD
        );
    }
    
    public function setSubject ($subject) {
        if (is_string($subject) || is_object($subject) || is_numeric($subject)) {
            return $this->subject = trim("$subject");
        }
        
        return false;
    }
    
    public function getSubject () {
        return $this->subject();
    }
    
    public function composeAll () {
        if (false === $this->searchHeader("Subject")) {
            $this->prependHeader('Subject', MIMEHeaderEncoding::encode($this->subject));
        }
        
        if (count($this->ccList) > 0) {
            $this->prependHeader('Cc', implode(",\r\n ", $this->ccList));
        }
        
        if (count($this->toList) > 0) {
            $this->prependHeader('To', implode(",\r\n ", $this->toList));
        } elseif ($this->searchHeader("Cc")) {
            // do nothing
        } elseif (!$this->searchHeader("To")) {
            $this->prependHeader('To', "Undisclosed recipients:;");
        }
        
        if ($this->searchHeader("From")) {
            // do nothing
        } elseif ($this->from instanceof EmailAddress) {
            $this->prependHeader('From', "{$this->from}");
        }
        
        if ($this->searchHeader("Reply-To")) {
            // do nothing
        } elseif ($this->replyTo instanceof EmailAddress) {
            $this->prependHeader('Reply-To', "{$this->replyTo}");
        }
        
        if (!$this->searchHeader("MIME-Version")) {
            $this->prependHeader('MIME-Version', '1.0');
        }
    }
}

// vim: ts=4 et ai

